#include "GameScene.hpp"

GameScene::GameScene() {
    isFontReady = false;
}

void GameScene::Load() {
    #ifdef _DEBUG
    std::cout << "GameScene::Load" << std::endl;

    mousePositionString = "X: 0, Y: 0";
    #endif

    paddleCenter.x = 300;
    paddleCenter.y = 500;

    paddleMaxSpeed = 200.0f;
    paddleAcceleration = 10.0f;
    paddleFraction = 0.3f;

    paddleHalfWidth = 50;
    paddleHalfHeight = 10;

    if (!isFontReady) {
        font.LoadFromFile("arial.ttf");
    }
}

void GameScene::Unload() {
    #ifdef _DEBUG
    std::cout << "GameScene::Unload" << std::endl;
    #endif
}

void GameScene::Update(sf::Event& event, sf::RenderWindow& app) {
    #ifdef _DEBUG
    // Mouse position
    if (event.Type == sf::Event::MouseMoved) {
        std::stringstream stringBuilder;
        stringBuilder << "X: " << event.MouseMove.X << ", "
                      << "Y: " << event.MouseMove.Y;

        mousePositionString = stringBuilder.str();
    }

    // Velocity
    std::stringstream stringBuilder;
    stringBuilder << "X: " << paddleVelocity.x << ", "
                  << "Y: " << paddleVelocity.y;

    paddleVelocityString = stringBuilder.str();
    #endif

    if (event.Type == sf::Event::KeyPressed) {
        if (event.Key.Code == sf::Key::Left) {
            paddleVelocity.x -= paddleAcceleration;
        } else if (event.Key.Code == sf::Key::Right) {
            paddleVelocity.x += paddleAcceleration;
        }
    }

    // Change type of paddle
    if (event.Type == sf::Event::KeyReleased) {
        if (event.Key.Code == sf::Key::A) {
            paddleCenter.x = 300;
            paddleCenter.y = 500;

            paddleMaxSpeed = 200.0f;
            paddleAcceleration = 10.0f;
            paddleFraction = 0.3f;

            paddleHalfWidth = 50;
            paddleHalfHeight = 10;
        } else if (event.Key.Code == sf::Key::B) {
            paddleCenter.x = 300;
            paddleCenter.y = 500;

            paddleMaxSpeed = 300.0f;
            paddleAcceleration = 200.0f;
            paddleFraction = 0.2f;

            paddleHalfWidth = 50;
            paddleHalfHeight = 10;
        } else if (event.Key.Code == sf::Key::C) {
            paddleCenter.x = 400;
            paddleCenter.y = 500;

            paddleMaxSpeed = 100.0f;
            paddleAcceleration = 1.5f;
            paddleFraction = 0.5f;

            paddleHalfWidth = 100;
            paddleHalfHeight = 10;
        }
    }

    if (paddleVelocity.x > 0) {
        paddleVelocity.x -= paddleFraction;

        if (paddleVelocity.x < 0.5f) {
            paddleVelocity.x = 0;
        }
    }

    if (paddleVelocity.x < 0) {
        paddleVelocity.x += paddleFraction;

        if (paddleVelocity.x > -0.5f) {
            paddleVelocity.x = 0;
        }
    }

    if (paddleVelocity.x > paddleMaxSpeed) {
        paddleVelocity.x = paddleMaxSpeed;
    }

    if (paddleVelocity.x < -paddleMaxSpeed) {
        paddleVelocity.x = -paddleMaxSpeed;
    }

    paddleCenter.x += app.GetFrameTime() * paddleVelocity.x;
}

void GameScene::Draw(sf::RenderWindow& app) {
    app.Clear(sf::Color(0, 0, 0));

    #ifdef _DEBUG
    // Mouse position
    sf::String mousePositionDebug(mousePositionString, font, 12);
    mousePositionDebug.Move(5.0f, 5.0f);

    app.Draw(mousePositionDebug);

    // Velocity
    sf::String paddleVelocityDebug(paddleVelocityString, font, 12);
    paddleVelocityDebug.Move(5.0f, 30.0f);

    app.Draw(paddleVelocityDebug);

    // Type of paddle
    sf::String paddleTypeDebug("A = Normal Paddle\nB = Slippery Paddle\nC = Long, but slow", font, 12);
    paddleTypeDebug.Move(5.0f, 55.0f);

    app.Draw(paddleTypeDebug);
    #endif

    // Draw paddle
    sf::Shape paddleRect = sf::Shape::Rectangle(paddleCenter.x - paddleHalfWidth,
                                                paddleCenter.y - paddleHalfHeight,
                                                paddleCenter.x + paddleHalfWidth,
                                                paddleCenter.y + paddleHalfHeight,
                                                sf::Color(255, 255, 255));
    app.Draw(paddleRect);
}

